 
/**
 * 对账结算单中自动金额按钮方法
 * @param obj
 * @return
 */
function autoMoney(obj){
	var prefix = $(obj).attr('pre');
	var allRows = $('#'+prefix+'_addItemDatagrid').datagrid('getRows');
	if(allRows.length==0){
		$.messager.show({title:"操作提示",msg:"亲，明细表中没有数据哦！",showType:'slide',timeout: 2000,showSpeed:400});
	}else{
		for(var i = 0;i<allRows.length;i++){
			$('#'+prefix+'_addItemDatagrid').datagrid('endEdit',i);
			allRows[i].NOW_MONEY = allRows[i].REAL_MONEY;
			$('#'+prefix+'_addItemDatagrid').datagrid('beginEdit',i);
			$('#'+prefix+'_addItemDatagrid').datagrid('endEdit',i);
		}
	}
}
/**
 * 计算本期金额和本期总额
 * @return
 */
//function jeChange(){
//	var verify = $('#cjs_addWinForm').find('input[qucln=IS_VERIFY]').val();
//	if(verify != '1'){
//		var sqjy = $('#cjs_addWinForm').find('input[qucln=LAST_MONEY]').next().val(); //上期结余
//		if(sqjy == null || sqjy == ''){
//			sqjy = 0;
//		}else{
//			sqjy = (parseFloat(sqjy)).toFixed(2);
//			$('#cjs_addWinForm').find('input[qucln=LAST_MONEY]').val(sqjy);
//			$('#cjs_addWinForm').find('input[qucln=LAST_MONEY]').attr('chan','T');
//			$('#cjs_addWinForm').find('input[qucln=LAST_MONEY]').next().val(sqjy);
//		}
//		var syys = $('#cjs_addWinForm').find('input[qucln=USE_MONEY]').next().val();  //使用预收
//		
//		if(syys == null || syys == ''){
//			syys = 0;
//		}else{
//			syys = (parseFloat(syys)).toFixed(2);
//			$('#cjs_addWinForm').find('input[qucln=USE_MONEY]').val(syys);
//			$('#cjs_addWinForm').find('input[qucln=USE_MONEY]').attr('chan','T');
//			$('#cjs_addWinForm').find('input[qucln=USE_MONEY]').next().val(syys);
//		}
//		var bdje = 0;//本单金额
//		var bqze = 0;//本期总额
//		var allRows = $('#cjs_addItemDatagrid').datagrid('getRows');
//		if(allRows.length == 0){
//			bdje = 0.00;
//		}else{
//			for(var i =0;i<allRows.length;i++){
//				if(allRows[i]["NOW_MONEY"] != null && allRows[i]["NOW_MONEY"] != ''){
//					bdje = parseFloat(bdje) + parseFloat(allRows[i]["NOW_MONEY"]);
//					bdje = (parseFloat(bdje)).toFixed(2);						//本单金额
//				}
//			}
//		}
//		$('#cjs_addWinForm').find('input[qucln=THIS_MONEY]').val(bdje);
//		$('#cjs_addWinForm').find('input[qucln=THIS_MONEY]').attr('chan','T');
//		$('#cjs_addWinForm').find('input[qucln=THIS_MONEY]').next().val(bdje);
//		//本期总额 = 上期结余 -使用预收+本单金额
//		bqze = parseFloat(sqjy) - parseFloat(syys) + parseFloat(bdje);
//		bqze = bqze.toFixed(2);
//		$('#cjs_addWinForm').find('input[qucln=THIS_YF_MONEY]').val(bqze);
//		$('#cjs_addWinForm').find('input[qucln=THIS_YF_MONEY]').attr('chan','T');
//		$('#cjs_addWinForm').find('input[qucln=THIS_YF_MONEY]').next().val(bqze);//本期总额
//		//本期应收 = 本单金额 - 使用预收
//		var bqys = bdje -  parseFloat(syys) ; 
//		bqys = bqys.toFixed(2);
//		$('#cjs_addWinForm').find('input[qucln=THIS_YS_MONEY]').val(bqys);  //本期应收
//		$('#cjs_addWinForm').find('input[qucln=THIS_YS_MONEY]').attr('chan','T');
//		$('#cjs_addWinForm').find('input[qucln=THIS_YS_MONEY]').next().val(bqys); 
//	}
//}


//计算公式的方法
function jeChange(){
	var bqys = 0;//本期应收
	var allRows = $('#cjs_addItemDatagrid').datagrid('getRows');
	if(allRows.length == 0){
		bqys = 0.00;
	}else{
		for(var i =0;i<allRows.length;i++){
			if(allRows[i]["NOW_MONEY"] != null && allRows[i]["NOW_MONEY"] != ''){
				bqys = parseFloat(bqys) + parseFloat(allRows[i]["NOW_MONEY"]);
				bqys = (parseFloat(bqys)).toFixed(2);						
			}
		}
	}
	$('#cjs_addWinForm').find('input[qucln=THIS_YS_MONEY]').val(bqys);  //本期应收
	$('#cjs_addWinForm').find('input[qucln=THIS_YS_MONEY]').attr('chan','T');
	$('#cjs_addWinForm').find('input[qucln=THIS_YS_MONEY]').next().val(bqys); 
	var sqjy = $('#cjs_addWinForm').find('input[qucln=LAST_MONEY]').next().val();  //上期结余
	var skcy = $('#cjs_addWinForm').find('input[qucln=CHARGE_CHAYI]').next().val();  //收款差异	
	var zys = parseFloat(bqys) + parseFloat(sqjy)+ parseFloat(skcy);
	$('#cjs_addWinForm').find('input[qucln=THIS_YSK]').next().val(parseFloat(zys).toFixed(2)); //总应收（应收款 = 上期结余+本期应收+收款差异）THIS_YSK
}


//选择单据弹出窗口的回调函数
function owQuery(){
	$('#cjz_dataQuery').attr('onclick','cjsQuery(this)');
}


//选择单据中的确定按钮
function _cjz_sure(){
	var jsId = $('#cjs_addWinTable input[qucln=JS_ID]').val();
	var selRows = $('#cjz_DATA_DATALIST').datagrid('getSelections');
	var allRows=$('#cjs_addItemDatagrid').datagrid("getRows");
	//销售对账明细中的数据最多不超过600条
	if(parseInt(allRows.length)+parseInt(selRows.length)>2000){
		$.messager.alert('温馨提示','亲，,对账单明细最多能录入1000条');
		return;
	}
	$('#cjz_DATA_PARENT_PANEL').window('close');
	for ( var i = 0; i < selRows.length; i++) {
		$('#cjs_addItemDatagrid').datagrid('appendRow',{
			 JS_ID:jsId,
			 YW_ID:selRows[i].YW_ID,
			 YW_CODE:selRows[i].YW_CODE,
			 TOTAL_MONEY:selRows[i].TOTAL_MONEY,
			 REAL_MONEY:selRows[i].REAL_MONEY,
			 C_DISCOUNT:selRows[i].C_DISCOUNT,
			 NOW_MONEY:selRows[i].REAL_MONEY,
			 YW_INPUT_DATE:selRows[i].YW_INPUT_DATE
		})
	}
	//调用结算方法
	jeChange();
}


//客户改变的时候删除明细中的行，并且把主表中的金额删除
function kelqje(){
	var clientId = $('#cjs_addWinForm').find('input[qucln=CLIENT_ID]').val(); //客户Id
	var dzjsoid=$('#cjs_addWinForm').find('input[qucln=DJ_CODE]').val()//单据id
	var dzjsOtime=$('#cjs_addWinForm').find('input[qucln=jsMonth]').val()//对账日期
	$.ajax({
		type:'post',
		url:_m_url+'fxa/jzDataByClient',
		data:{'clientId':clientId,"dzjsoid":dzjsoid,"dzjsotime":dzjsOtime},
		success:function(data){
			var sqjy = data.sqjy;
			var ysjy = data.ysjy;
			var skcy = data.skcy;
			var zqk = data.zqk;
			if(sqjy == null || sqjy == ''){
				sqjy = 0;
			}else{
				sqjy = (parseFloat(sqjy)).toFixed(2);
			}
			if(ysjy == null || ysjy == ''){
				ysjy = 0;
			}else{
				ysjy = (parseFloat(ysjy)).toFixed(2);
			}
			if(skcy == null || skcy == ''){
				skcy = 0;
			}else{
				skcy = (parseFloat(skcy)).toFixed(2);
			}
			if(zqk == null || zqk == ''){
				zqk = 0;
			}else{
				zqk = (parseFloat(zqk)).toFixed(2);
			}
			$('#cjs_addWinForm').find('input[qucln=LAST_MONEY]').val(sqjy); 		//上期结余
			$('#cjs_addWinForm').find('input[qucln=LAST_MONEY]').attr('chan','T'); //上期结余
			$('#cjs_addWinForm').find('input[qucln=LAST_MONEY]').next().val(sqjy); //上期结余
			
			$('#cjs_addWinForm').find('input[qucln=PRE_MONEY]').val(ysjy);
			$('#cjs_addWinForm').find('input[qucln=PRE_MONEY]').attr('chan','T');
			$('#cjs_addWinForm').find('input[qucln=PRE_MONEY]').next().val(ysjy);  //预收结余
			
			$('#cjs_addWinForm').find('input[qucln=CHARGE_CHAYI]').val(skcy); //收款差异
			$('#cjs_addWinForm').find('input[qucln=CHARGE_CHAYI]').attr('chan','T'); //收款差异
			$('#cjs_addWinForm').find('input[qucln=CHARGE_CHAYI]').next().val(skcy); //收款差异
			
			$('#cjs_addWinForm').find('input[qucln=THIS_ZQK]').val(zqk); //总欠款THIS_YSK
			$('#cjs_addWinForm').find('input[qucln=THIS_ZQK]').attr('chan','T'); //总欠款THIS_YSK
			$('#cjs_addWinForm').find('input[qucln=THIS_ZQK]').next().val(zqk); //总欠款THIS_YSK
			//删除明细中存在的行
			var allRows = $('#cjs_addItemDatagrid').datagrid('getRows');
			for(var i=allRows.length-1;i>=0;i--){
				$('#cjs_addItemDatagrid').datagrid("deleteRow",i);
			}
		}
	})
	getClientMoney();
}
//对账中根据用户的Id在银行到款中查找，并且显示用户的到款金额
function getClientMoney(){
	var clientId=$('#cjs_addWinForm').find('input[qucln=CLIENT_ID]').val(); //客户Id
	
	$.ajax({
		type:'post',
		url:_m_url+'fxa/DKMByClient',
		data:{'clientId':clientId},
		success:function(data){
			$('#cjs_addWinForm').find('input[qucln=DKK_MONEY]').val(data[0].DKMONEY); 
			$('#cjs_addWinForm').find('input[qucln=DKK_MONEY]').attr('chan','T'); 
			$('#cjs_addWinForm').find('input[qucln=DKK_MONEY]').next().val(data[0].DKMONEY); 
		}
	})
	
}

//对账结算单 中的 选择单据按钮的方法
function chooseBills(obj){
	var dsId = $(obj).attr('dsId');
	var clientId = $('#cjs_addWinForm').find('input[qucln=CLIENT_ID]').val(); //客户Id
	if(clientId == null || clientId == ''){
		$.messager.alert('提示','请选择客户！');
		return;
	}
	var allRows = $('#cjs_addItemDatagrid').datagrid('getRows');
	var ywIds = "";
	if(allRows.length != 0){
		for(var i=0;i<allRows.length;i++){
			ywIds +="'"+allRows[i].YW_ID+"'";
			if(i<allRows.length-1){
				ywIds+=',';
			}
		}
	}
	//不展示现有的数据，等查询按钮查询时才展示
	_data_qw('cjz','ds_bs_dzxzdj','cjz_sure1','44','owQuery','');//根据现成的数据展现窗口
}

//结算清单的选择单据的查询按钮
function cjsQuery(obj){
	var prefix=$(obj).attr('pre');
	var dsId=$(obj).attr('dsid');
	var resultDG = 'DATA_DATALIST';
	var comUseConf='commUseD';
	var clientId = $('#cjs_addWinForm').find('input[qucln=CLIENT_ID]').val(); //客户Id
	var otherConfig = "1-CLIENTS_ID-"+clientId+"-inc|+"; 
	var comUse = parseConfig(prefix+"_"+comUseConf);
	var useConf = otherConfig+comUse;
	//获取对账明细的数据行
	var dzmxRows = $('#cjs_addItemDatagrid').datagrid('getRows');
	var ids='';
	if(dzmxRows.length>0){
		for(var i=0,j=dzmxRows.length;i<j;i++){
			ids+="'"+dzmxRows[i].YW_ID+"',";
		}
		ids=ids.substring(0,ids.lastIndexOf(","));
		ids=" and YW_ID not in("+ids+")";
	}
	//将YW_ID组成一个'YW_ID',字符串
	//将datagrid 获取到后 就用变量存起来,不然每次 jquery 获取都是一次耗时操作
	var queryDG=$('#'+prefix+"_"+resultDG);
	queryDG.datagrid({
			idField:"YW_ID",
			pageNumber:1,
			pagination:true,
			pageList:[300],
			url:_m_url+'framework/loadDataQ',
			queryParams:{'dsId':dsId,'config':useConf,"originalSql":ids}
	});
}


//删除按钮
function dzjsRowDel(prefix){
	itemRowDel(prefix);
	jeChange();
}


//重写保存按钮
function dzjsORSave(obj){
	//客户Id
	var clientId = $('#cjs_addWinForm').find('input[qucln=CLIENT_ID]').val();
	var sou = $(obj).attr('sou');
	//判断当前客户是否已经存在保存状态的对账单
	$.ajax({
		type:'post',
		url:_m_url+'fxa/dzjsByClient',
		data:{'clientId':clientId},
		success:function(data){
			if(sou == '2'){
				 common_save(obj);
			}else{
				if(data == 0){
					 common_save(obj);
				}else{
					$.messager.alert('温馨提示','亲，当前客户已经存在一条未审核状态的单据！');
				}
			}
		}
	})
}
////请求存储过程返回根据账期查询的数据
//function getTcData(){
//	//客户Id
//	var clientId =  $('#cjs_addWinForm').find('input[qucln=CLIENT_ID]').val();
////	var dataObj = {};
////	$.ajax({
////		type:'post',
////		url:_m_url+'fxa/getTcData',
////		data:{'clientId':clientId},
////		async : false,
////		success:function(data){
////			dataObj = data;
////		}
////	});
//	$('#cjs_addItemDatagrid').datagrid({
//		url:_m_url+'fxa/getXsDzTcData',
//		queryParams:{'clientId':clientId},
//		onLoadSuccess:function(data){
//			//把所有的行变成更新行
//			
//		}
//	});
////	return dataObj;
//}
////生成对账单明细的按钮方法
//function newDZ(){
//	var dataObj = getTcData();
//	var flag = dataObj.flag;
//	var dataList = dataObj.dataList;
//	var rows = $('#cjs_addItemDatagrid').datagrid('getRows');
//	if(dataList.length == 0){
//		if (rows.length > 0) {
//			for ( var i =rows.length-1; i >=0; i--) {
//				$('#cjs_addItemDatagrid').datagrid('deleteRow',i);
//			}
//		}
//		$.messager.alert('温馨提示','该客户截止到上月底没有需对账的业务数据！');
//	}else{
//		if (rows.length > 0) {
//			for ( var i =rows.length-1; i >=0; i--) {
//				$('#cjs_addItemDatagrid').datagrid('deleteRow',i);
//			}
//		}
//		for ( var i = 0; i < dataList.length; i++) {
//			$('#cjs_addItemDatagrid').datagrid('appendRow', {
//				 YW_ID:dataList[i].YW_ID,
//				 YW_CODE:dataList[i].YW_CODE,
//				 TOTAL_MONEY:dataList[i].TOTAL_MONEY,
//				 REAL_MONEY:dataList[i].REAL_MONEY,
//				 C_DISCOUNT:dataList[i].C_DISCOUNT,
//				 NOW_MONEY:dataList[i].REAL_MONEY,
//				 YW_INPUT_DATE:dataList[i].YW_INPUT_DATE,
//				 YW_MEMO:dataList[i].YW_MEMO
//			});
//		}
//
//	}
//}
/**
 * 对账结算单生成开票单
 * 
 * @param obj
 * @return
 */
function jsNewKP(obj){
	var pre = $(obj).attr('pre');  // 获取前缀
	var dsId = 'ds_fx_invoice_kp1;ds_fx_invoice_kp_item1'; // 生成开票单的dsId
	var jsId = $('#'+pre+'_addWinForm input[name=jsId]').val();
	var verify = $('#'+pre+'_addWinForm input[name=isVerify]').val();
	if(jsId == null || jsId ==""){
		$.messager.alert("温馨提示","单据尚未保存，不能生成开票单！");
		return;
	}
	if(verify !='1' || verify == null || verify ==''){
		$.messager.alert('提示','单据未审核，不能进行此操作！');
		return;
	}
	$.ajax( {
		type : 'post',
		url : _m_url+'newcwCost/jsNewKP',  
		data : {'jsId' :jsId},
		success : function(data) {
			if(data.info == 'fail'){
				$.messager.alert("提示",data.msg);
			}else{
				$.messager.alert("温馨提示","亲，开票单已生成");
				pdkpStatus();
			}
		}
	});
}
//根据开票状态判断按钮
function pdkpStatus(){
	var isinv = $('#cjs_addWinTable input[qucln=IS_INV]').val();//开票状态
	if(isinv == '1'){
		$('#cjs_M_UBUTN a[id=cjs_newKP]').linkbutton("disable");  //开票按钮
	}else{
		$('#cjs_M_UBUTN a[id=cjs_newKP]').linkbutton("enable");  //开票按钮
	}
	var clientId =  $('#cjs_addWinTable input[qucln=CLIENT_ID]').val();//客户Id
	var str = 'cId-'+clientId+';';
	$('#cjs_spd a[id=cjs_a]').attr('config',str); // 打印
}




//重写返回事件
function dzjsRollBack(obj){
	var closeFlag=true;   // 返回false就取消关闭
	var prefix=$(obj).attr("pre");
	var sflag=$(obj).attr("sflag");
	var saveObj = $('#'+prefix+"_M_UBUTN a[id="+prefix+"_SAVE_BUT]");
	var keyVaules_m = parsePKStr(prefix+"_addWinForm");//主明细的表单Id
	if(sflag=='m'){    // 主明细
		var m_key=keyVaules_m.split("-")[0];
		var m_val=keyVaules_m.split("-")[1];
		if(typeof(m_val)=='undefined'||m_val==""||m_val==undefined||m_val=='undefined'){   // 添加页面
			$.messager.confirm("操作提示", "数据已经改变，您确认保存吗？", function (data) {
				if (data){
					$(obj).attr("flag","1");
					$(obj).attr("oflag","1");
					$(obj).attr("sou","1");
					$(obj).attr("isBack","true")
					dzjsORSave(saveObj);  // 确认按钮调用保存事件
					closeFlag=false;
				}else{
					$('#'+prefix+'_m_parentPanel').window('close');  // 取消直接关闭
				}
			});
		}else{    // 详情页面
			// 遍历页面所有元素判断是否有元素发生改变
			var values=getFormJsons(prefix+"_addWinForm");    // 得到主表的数据
			var changes=$('#'+prefix+"_addItemDatagrid").datagrid('getChanges');   // 得到明细的数据
			if(values==0&&changes==0){
				$('#'+prefix+'_m_parentPanel').window('close');  // 没有修改表单任何元素的时候直接关闭
			}else{   // 有修改元素的时候
				$.messager.confirm("操作提示", "数据已经改变，您确认保存吗？", function (data) {
					if (data){
						$(obj).attr("flag","1");
						$(obj).attr("oflag","1");
						$(obj).attr("sou","2");
						$(obj).attr("isBack","true")
						dzjsORSave(saveObj);  // 确认按钮调用保存事件
						closeFlag=false;
					}else{
						$('#'+prefix+'_m_parentPanel').window('close');  // 取消直接关闭
					}
				});
			}
		}
	}
	
	
	
}